package com.shopping.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shopping.auto.JSONAuthentication;
import com.shopping.entity.ShProduct;
import com.shopping.entity.ShUser;
import com.shopping.mapper.ShProductMapper;
import com.shopping.response.Result;
import com.shopping.utils.UpPhotoNameUtils;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.crypto.Data;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author shopping
 * @since 2022-03-09
 */
@RestController
@RequestMapping("/sh-product")
public class ShProductController extends JSONAuthentication {
    @Autowired
    ShProductMapper shProductMapper;
    @ApiOperation(value = "添加商品")
    @PostMapping("/add")
    public void addUser(@RequestParam("file") MultipartFile file, HttpServletRequest request, HttpServletResponse response, @RequestParam("name") String name, @RequestParam("kind") String kind, @RequestParam("price") float price, @RequestParam("phone") String phone) throws  Exception{
        ShProduct shProduct=new ShProduct();
        shProduct.setName(name);
        shProduct.setKind(kind);
        shProduct.setPrice(price);
        Date date=new Date();
        Timestamp timestamp=new Timestamp(date.getTime());
        shProduct.setCreatTime(timestamp);
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("name",shProduct.getName());
        ShProduct a= shProductMapper.selectOne(wrapper);
        if(a!=null){
            Result result=Result.error();
            result.setMessage("该商品名已存在已存在");
            this.WriteJSON(request, response, result);
        }else {
            byte[] bytes = file.getBytes();
            String imageFileName = file.getOriginalFilename();
            String fileName = UpPhotoNameUtils.getPhotoName("img",imageFileName);
            Path path = Paths.get("D:\\自己的项目\\流浪动物救助\\adopt\\adopt\\src\\assets\\picture\\" + fileName);
            //“C:\\框架\\D4\\d4_pc_ui\\src\\assets\\images\\img\\”为本地目录
            Files.write(path, bytes);//写入文件
            String avatar_url=fileName;
            shProduct.setPicture(avatar_url);
            response.reset();

            int i=shProductMapper.insert(shProduct);
            if(i>0){
                Result ok = Result.ok();
                this.WriteJSON(request, response, ok);
            } else {
                Result error = Result.error();
                this.WriteJSON(request, response, error);
            }
        }

    }
    @ApiOperation(value = "删除商品")
    @PostMapping("/del")
    public void delUser(HttpServletRequest request, HttpServletResponse response, @RequestParam("userId") int userId ) throws Exception{
        int i=shProductMapper.deleteById(userId);
        if(i>0){
            Result ok = Result.ok();
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "修改商品价格")
    @PostMapping("/updPrPrice")
    public void updUser(HttpServletRequest request, HttpServletResponse response, @RequestParam("id") int id,@RequestParam("price") float price)throws Exception{
        ShProduct shProduct=shProductMapper.selectById(id);
        shProduct.setPrice(price);
        if(shProductMapper.updateById(shProduct)>0){
            Result ok = Result.ok();
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "修改商品类型")
    @PostMapping("/updPrType")
    public void updPrType(HttpServletRequest request, HttpServletResponse response, @RequestParam("id") int id,@RequestParam("type") String  type)throws Exception{
        ShProduct shProduct=shProductMapper.selectById(id);
        shProduct.setKind(type);
        if(shProductMapper.updateById(shProduct)>0){
            Result ok = Result.ok();
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "通过商品名查询商品")
    @PostMapping("/findByName")
    public void findById(HttpServletRequest request, HttpServletResponse response,@RequestParam("name") String  name,@RequestParam("size") int size,@RequestParam ("current") int current)throws  Exception{
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.like("name",name);
        Page<ShProduct> shUserPage=new Page<>(current,size);
        Page<ShProduct> shUserPages=shProductMapper.selectPage(shUserPage,wrapper);
        List<ShProduct> records = shUserPages.getRecords();
        if(records.size()>0){
            Result ok = Result.ok().data("product",records);
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "通过商品类型查询商品")
    @PostMapping("/findByType")
    public void findByType(HttpServletRequest request, HttpServletResponse response,@RequestParam("name") String  name,@RequestParam("size") int size,@RequestParam ("current") int current)throws  Exception{
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.like("name",name);
        Page<ShProduct> shUserPage=new Page<>(current,size);
        Page<ShProduct> shUserPages=shProductMapper.selectPage(shUserPage,wrapper);
        List<ShProduct> records = shUserPages.getRecords();
        if(records.size()>0){
            Result ok = Result.ok().data("product",records);
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "通过商品价格降序查询商品")
    @PostMapping("/findByPrice")
    public void findByPrice(HttpServletRequest request, HttpServletResponse response,@RequestParam("name") String  name,@RequestParam("size") int size,@RequestParam ("current") int current)throws  Exception{
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.orderByDesc("picture");
        Page<ShProduct> shUserPage=new Page<>(current,size);
        Page<ShProduct> shUserPages=shProductMapper.selectPage(shUserPage,wrapper);
        List<ShProduct> records = shUserPages.getRecords();
        if(records.size()>0){
            Result ok = Result.ok().data("product",records);
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "查询全部商品")
    @PostMapping("/findAllProduct")
    public void findAllProduct(HttpServletRequest request, HttpServletResponse response,@RequestParam("name") String  name,@RequestParam("size") int size,@RequestParam ("current") int current)throws  Exception{
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.orderByDesc("picture");
        Page<ShProduct> shUserPage=new Page<>(current,size);
        Page<ShProduct> shUserPages=shProductMapper.selectPage(shUserPage,wrapper);
        List<ShProduct> records = shUserPages.getRecords();
        if(records.size()>0){
            Result ok = Result.ok().data("product",records);
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
}

